<?php

namespace Api\Captcha;

use Api\RepositoryResourceTrait;

/**
 * 资源仓库
 */
class CaptchaRepository
{
    use RepositoryResourceTrait;

    /**
     * 构造方法
     */
    public function __construct($captcha)
    {
        $this->captcha = $captcha;
    }

    /**
     * 图形验证码信息
     *
     * @param  string $urlDomain 域名
     */
    public function getCaptchaInfo($urlDomain)
    {
        return $this->_getCaptchaInfo($urlDomain);
    }

    /**
     * 验证图形验证码
     * @param  string $captchaCode 图形验证码
     * @param  string $captchaId   图形验证码UUID
     */
    public function checkCaptcha($captchaCode, $captchaId)
    {
        $pattern = '/^[0-9a-zA-Z]+$/';
        if (preg_match($pattern, $captchaCode) === 0) {
            throw_exception('Captcha', 'CheckFail', trans('authPerson.GraphicExc'));
        }
        $result = $this->_checkCaptcha($captchaCode, $captchaId);
        if ($result === false) {
            throw_exception('Captcha', 'CheckFail', trans('authPerson.GraphicFailed'), '[captchaCode:' . $captchaCode . ', captchaId:' . $captchaId . ']');
        }
    }

}
